{% load static %}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="{% static 'admin/css/pearCommon.css' %}" />
    <link rel="stylesheet" href="{% static 'component/layui/css/layui.css' %}" />

</head>

<body>
    <form class="layui-form" action="">
        {% csrf_token %}
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <div class="layui-form-item">
                        <label class="layui-form-label">用户</label>
                        <div class="layui-input-block">
                            <select name="user_id" lay-verify="required" lay-filter="user" lay-search="">
                                <option value=""></option>
                                {% for user in users %}
                                    <option value="{{user.id}}">{{ user.name }} ---[身份证号:{{ user.id_card }}]</option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">角色</label>
                        <div class="layui-input-block">
                            <select name="role_id" lay-verify="required" lay-filter="role" lay-search="">
                                <option value=""></option>
                                {% for role in roles %}
                                <option value="{{role.id}}">{{ role.name }}</option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">仓库</label>
                        <div class="layui-input-block">
                            {% for store in stores %}
                                <input type="checkbox" title="{{ store.name }}" value="{{ store.id }}" lay-skin="primary">
                            {% endfor %}
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit=""
                    lay-filter="user-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>

    <script src="{% static 'component/layui/layui.js' %}"></script>
    <script src="{% static 'component/my.js' %}"></script>

    <script>
        layui.use(['form', 'jquery'], function () {
            let form = layui.form;
            let $ = layui.jquery;
            form.on('submit(user-save)', function (data) {
                // 选中的仓库
                var stores = [];
                $('input[type=checkbox]:checked').each(function () {
                    stores.push($(this).val());
                });
                //数组
                console.log(stores);
                data.field.stores = stores

                $.ajax({
                    url: "{% url 'user:permission_add' %}",
                    data: JSON.stringify(data.field),
                    dataType: 'json',
                    contentType: 'application/json',
                    type: 'post',
                    headers: {
                        "X-CSRFToken": getCookie("csrftoken")
                    },
                    success: function (result) {
                        console.log(result)
                        if (result.status == 1) {
                            layer.msg(result.msg, { icon: 1, time: 1000 }, function () {
                                parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                                parent.layui.table.reload("user-table");
                            });
                        } else {
                            layer.msg(result.msg, { icon: 2, time: 3000 });
                        }
                    }
                })
                return false;
            });
        })
    </script>
    <script>
    </script>
</body>

</html>